package cn.xin.system.service.impl;

import cn.xin.base.query.BaseQuery;
import cn.xin.base.util.BussinessException;
import cn.xin.base.util.PageList;
import cn.xin.system.domain.Dictionarydetail;
import cn.xin.system.mapper.DictionarydetailMapper;
import cn.xin.system.service.DictionarydetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DictionarydetailServiceImpl implements DictionarydetailService {

    @Autowired
    private DictionarydetailMapper dictionarydetailMapper;

    @Override
    public List<Dictionarydetail> findAll() {
        return dictionarydetailMapper.findAll();
    }

    @Override
    public Dictionarydetail findById(Long id) {
        return dictionarydetailMapper.findById(id);
    }

    @Override
    public void add(Dictionarydetail dictionarydetail) {
        dictionarydetailMapper.add(dictionarydetail);
    }

    @Override
    public void update(Dictionarydetail dictionarydetail) {
        dictionarydetailMapper.update(dictionarydetail);
    }

    @Override
    public void delete(Long id) {
        if(id == null){
            throw new BussinessException("id为空");
        }
        dictionarydetailMapper.delete(id);
    }

    @Override
    public PageList<Dictionarydetail> queryByPage(BaseQuery query) {
        Integer totals = dictionarydetailMapper.queryByCount(query);

        if (totals == 0 || totals == null){
            return new PageList<Dictionarydetail>(0,new ArrayList<>());
        }
        List<Dictionarydetail> rows = dictionarydetailMapper.queryByPage(query);

        List<Long> ids = rows.stream().map(row->row.getId()).collect(Collectors.toList());

        return new PageList<Dictionarydetail>(totals,rows);
    }

    @Override
    public void batchDelete(List<Long> ids) {
        dictionarydetailMapper.batchDelete(ids);
    }

    @Override
    public List<Dictionarydetail> findByDeptId(Long typeId) {
        return dictionarydetailMapper.findByDeptId(typeId);
    }
}
